package org.adorsys.aderp.aderplogin.roles;

import java.security.BasicPermission;

import org.apache.commons.lang3.StringUtils;

public class ContextualRole {

	private final BasicPermission permProf;
	private final BasicPermission context;
	
	public ContextualRole(String permProf, String context) {
		super();
		this.permProf = new SimplePermission(permProf);
		this.context = new SimplePermission(context);
	}
	
	public ContextualRole(String role){
		if(StringUtils.isBlank(role)) throw new IllegalStateException("Role can not be blank");
		String[] split = role.split(":");
		permProf = new SimplePermission(split[0]);
		if(split.length>1) {
			context = new SimplePermission(split[1]);
		} else {
			context = null;
		}
	}

	public BasicPermission getPermProf() {
		return permProf;
	}

	public BasicPermission getContext() {
		return context;
	}
	
	public static boolean isCompositeRole(String role){
		return role.contains(":");
	}
	
	public static String extractPermissionProfile(String role){
		if(StringUtils.isBlank(role)) throw new IllegalStateException("Role can not be blank");
		String[] split = role.split(":");
		return split[0];
	}

}
